package com.mesa.android.avatalk.mvvm.login.ui.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.mesa.android.avatalk.R
import com.mesa.android.avatalk.base.utils.DisplayUtils
import com.mesa.android.avatalk.base.utils.ResourcesUtils
import com.mesa.android.avatalk.databinding.PopmenuLanguageBinding
import com.mesa.android.avatalk.mvvm.login.viewmodel.LanguagePopMenuViewModel

/**
 * Author: [author]
 * Date: [date]
 * Description: 
 */
 
class [name]PopMenu(context: Context?) : PopupWindow(context), LifecycleOwner, ViewModelStoreOwner {

    private var mAppViewModelStore: ViewModelStore? = null
    private val mRegistry = LifecycleRegistry(this)
    private var binding: Popmenu[name]Binding ?= null
    private var m[name]ViewModel: [name]ViewModel?= null

    init {
        mAppViewModelStore = ViewModelStore()
        m[name]ViewModel = ViewModelProvider(this).get( [name]ViewModel::class.java)
        val view = LayoutInflater.from(context).inflate(R.layout.popmenu_[name], null)
        contentView = view
        binding = Popmenu[name]Binding.bind(view)
        binding?.vm = m[name]ViewModel
        this.isFocusable = true // 设置弹出窗口可
        isOutsideTouchable = true
        setBackgroundDrawable(null)
        
        mRegistry.currentState = Lifecycle.State.CREATED
        mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
        mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)

        setOnDismissListener {
            mRegistry.currentState = Lifecycle.State.DESTROYED
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
        }
    }

    override fun showAsDropDown(anchor: View?) {
        super.showAsDropDown(anchor, -ResourcesUtils.getDimen(R.dimen.language_popmenu_w) + (anchor?.width?:0), DisplayUtils.dp2px(6f))
    }
    
    override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore!!
    }

    override fun getLifecycle(): Lifecycle {
        return mRegistry
    }
}